// components/wait/wait.js
import {Config} from "../../utils/config";
// 小程序配置信息
let interstitialAd = null;
const app = getApp();
Component({
    /**
     * 组件的属性列表
     * app.globalData.serverData.config.waitPageImg,//
     * app.globalData.serverData.config.waitPageTxt,//
     */
    options:{
        addGlobalClass:true,
    },
    properties: {
        item: {
            type: Object,
            value: {
                img: '/images/loading.gif',
                text: '正在执行...'
            }
        },
        bannerAd: {
            type: Object,
            value: {
                enable: false
            }
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        index: 0
    },

    /**
     * 组件的方法列表
     */
    methods: {
        loadInterstitialAd(adUnitID) {
            if (wx.createInterstitialAd) {
                interstitialAd = wx.createInterstitialAd({ adUnitId: adUnitID })
                interstitialAd.onLoad(() => {
                    interstitialAd.show().catch((err) => {
                        this.showError({title: err,icon: 'error',duration: 2000,mask: true});
                    })
                })
                interstitialAd.onError((err) => {
                    this.showError({title: err,icon: 'error',duration: 2000,mask: true});
                })
            }
        }
    },
    lifetimes: {
        attached() {
            const bannerAd = app.globalData.serverData['ad'][Config.AD_POSITION_WAIT];
            if(bannerAd.enable){
                const num = bannerAd.customImg.length - 1
                this.setData({
                    index: Math.round(Math.random() * num),
                    bannerAd :bannerAd
                })
                // console.log("img",this.data.bannerAd.customImg[index].img,this.data.index)
                if (bannerAd.source == 1) {
                    adid =bannerAd.unitId
                    this.loadInterstitialAd(adid);
                }
            }
        }
    }

})
